﻿using System8Equations;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace t_System8Equations
{
    
    
    /// <summary>
    ///This is a test class for t_Solver and is intended
    ///to contain all t_Solver Unit Tests
    ///</summary>
    [TestClass()]
    public class t_Solver
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion

        [TestMethod]
        public void TestCTor()
        {
            var s = new Solver();
        }

        Solver.System8Parameters GetDefaultD0Params()
        {
            return new Solver.System8Parameters()
            {
                n = 497008,
                p = 26790,
                n1 = 60463,
                n2 = 213012,
                p1 = 7159,
                p2 = 16886,
                n12 = 36807,
                p12 = 5130
            };
        }

        Solver.System8Alphas GetDefaultAlphas()
        {
            return new Solver.System8Alphas()
            {
                alpha_1 = 1.0,
                alpha_2 = 0.0,
                alpha_3 = 1.0,
                alpha_4 = 1.0,
                alpha_5 = 1.0,
                alpha_6 = 1.0
            };
        }

        [TestMethod]
        public void TestSolverSimple()
        {
            ///
            /// The below test numbers were pulled from DZERO paper long long
            /// time ago (good to test it...).
            /// 

            var s = new Solver();
            var v = GetDefaultD0Params();
            var result = s.CalculateSystem8(
                v.n, v.p, v.n1, v.n2, v.p1, v.p2, v.n12, v.p12,
                1.0, 1.0, 1.0, 1.0, 1.0, 1.0);

            ///
            /// I hope this cross check is right. If not, then we need to invent a better
            /// test - but it will work for now.
            /// 

            result = CheckResults(result);
        }

        [TestMethod]
        public void TestCleanMethod()
        {
            var s = new Solver();
            var v = GetDefaultD0Params();
            var result = s.CalculateSystem8(v, GetDefaultAlphas());
            result = CheckResults(result);
        }

        [TestMethod]
        public void TestMultipleTiming()
        {
            var s = new Solver();
            var v = GetDefaultD0Params();
            for (int i = 0; i < 100; i++)
            {
                var result = s.CalculateSystem8(v, GetDefaultAlphas());
            }
        }

        private Solver.System8Results CheckResults(Solver.System8Results result)
        {
            Assert.IsTrue(CloseTo(result.epmistag1, 0.050516373700456967), "epmistag1 isn't quite right!?");
            Assert.IsTrue(CloseTo(result.epmistag2, 0.33001271512623803), "epmistag1 isn't quite right!?");
            Assert.IsTrue(CloseTo(result.eptag1, 0.34399663789016915), "epmistag1 isn't quite right!?");
            Assert.IsTrue(CloseTo(result.eptag2, 0.73669071849976586), "epmistag1 isn't quite right!?");
            return result;
        }

        private bool CloseTo(double found, double expected)
        {
            double delta = System.Math.Abs(found - expected);
            return (delta / expected) < 0.05;
        }

    }
}
